﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_AssemblyCheckFunctions : Form
    {
        public frm_Add_AssemblyCheckFunctions()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.showOnForm.Checked = true;
            LoadCheckType();
        }

        //////////////////////////////////////
        /// Load Check Type to List
        //////////////////////////////////////
        private void LoadCheckType()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT * FROM [231_CheckType] ORDER BY CheckTypeName";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["CheckTypeID"].ToString().Trim();
                    item.Text = dataReader["CheckTypeName"].ToString().Trim();
                    this.checkTypeID.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
            }
            catch
            {
                MessageBox.Show("Cannot load Check Type");
            }
            finally
            {
                myConnection.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }        
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            int checktypeid = 0;
            int functionid = 0;
            int functiontype = 0;
            int showform = 0;
            string functionname = null;
            string defaultvalue = null;

            if (this.checkTypeID.SelectedIndex < 0)
            {
                err = "Please select Check Type.";
            }
            else
            {
                ComboboxItem item = (ComboboxItem)this.checkTypeID.SelectedItem;
                checktypeid = Int32.Parse(item.Value.ToString());
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.functionID.Text.Trim()))
                {
                    err = "No function ID to create new.";
                }
                else
                {
                    functionid = Int32.Parse(this.functionID.Text.Trim());
                    if (functionid <= 0)
                    {
                        err = "No function ID to create new.";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.functionName.Text.Trim()))
                {
                    err = "Please enter Function Name";
                }
                else
                {
                    functionname = this.functionName.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (this.showOnForm.Checked)
                {
                    showform = 1;
                }
                else
                {
                    showform = 0;
                }

                functiontype = this.functionType.SelectedIndex + 1;
                defaultvalue = this.defaultValue.Text.Trim();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_CheckFuntions]"
                        + " (FunctionID, CheckTypeID, FunctionName, FunctionType, ShowForm, DefaultValue)"
                        + " VALUES (" + functionid + ", " + checktypeid + ", N'" + functionname + "',"
                        + " " + functiontype + ", " + showform + ", N'" + defaultvalue + "')";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    MessageBox.Show("Add Function successfully.");
                    this.functionName.Text = null;
                    this.defaultValue.Text = null;
                    myConnection.Close();
                    GenerateFunctionID();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot add Function: " + ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(err);
            }
        }

        private void checkTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            GenerateFunctionID();
        }

        private void GenerateFunctionID()
        {
            if (this.checkTypeID.SelectedIndex >= 0)
            {
                ComboboxItem checkid = (ComboboxItem)this.checkTypeID.SelectedItem;
                int max_id = 0;
                int new_id = 0;
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT MAX(FunctionID)"
                        + " FROM [231_CheckFuntions] WHERE CheckTypeID = " + checkid.Value.ToString();
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        if (dataReader[0] != DBNull.Value)
                        {
                            if (!Int32.TryParse(dataReader[0].ToString(), out max_id))
                            {
                                MessageBox.Show("Cannot create Function ID. Please try again.");
                            }
                        }
                    }
                    dataReader.Close();
                    sql_cmd.Dispose();

                    new_id = max_id + 1;
                    this.functionID.Text = new_id.ToString();
                }
                catch
                {
                    MessageBox.Show("Cannot create Check Function ID");
                }
                finally
                {
                    myConnection.Close();
                }
            }
        }

    }
}
